package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootTest
public class testRedis {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Test
    public void testString() {
        //set
        redisTemplate.opsForValue().set("name", "张三");
        //get
        String name = redisTemplate.opsForValue().get("name");
        System.out.println("name:"+name);
        //setex
        redisTemplate.opsForValue().set("name", "张三", 10, TimeUnit.SECONDS);
        //setnx
        redisTemplate.opsForValue().setIfAbsent("name", "张三");
    }
    @Test
    public void testHash(){
        //hset
        redisTemplate.opsForHash().put("user", "name", "张三");
        //hget
        redisTemplate.opsForHash().get("user", "name");
        //hkeys
        redisTemplate.opsForHash().keys("user");
        //hvals
        redisTemplate.opsForHash().values("user");
        //hdel
        redisTemplate.opsForHash().delete("user", "name");
    }
    @Test
    public void testList(){
        //lpush
        redisTemplate.opsForList().leftPush("list", "张三");
        //lrange
        redisTemplate.opsForList().range("list", 0, -1);
        //rpop
        redisTemplate.opsForList().rightPop("list");
        //lpop
        redisTemplate.opsForList().leftPop("list");
        //llen
        redisTemplate.opsForList().size("list");
    }
    @Test
    public void testSet(){
        //sadd
        redisTemplate.opsForSet().add("set", "张三", "张三1");
        redisTemplate.opsForSet().add("set2", "张三", "张三2");
        //smembers
        redisTemplate.opsForSet().members("set");
        //scard
        redisTemplate.opsForSet().size("set");
        //sinter
        redisTemplate.opsForSet().intersect("set", "set2");
        //sunion
        redisTemplate.opsForSet().union("set", "set2");
    }
    @Test
    public void testZSet(){
        //zadd
        redisTemplate.opsForZSet().add("zset", "张三", 1);
        redisTemplate.opsForZSet().add("zset", "张三1", 2);
        //zrange
        redisTemplate.opsForZSet().range("zset", 0, -1);
        //zincrby
        redisTemplate.opsForZSet().incrementScore("zset", "张三", 1);
        //zrem
        redisTemplate.opsForZSet().remove("zset", "张三");
    }
    @Test
    public void testPubSub(){
        //keys 所有的key
        redisTemplate.keys("*");
        //exists 存在
        redisTemplate.hasKey("name");
        //type 类型
        redisTemplate.type("name");
        //del 删除
        redisTemplate.delete("name");
    }
}
